<template>
  <div class="app-container">
    <div class="operation-container">
      <el-button class="filter-item" type="primary" round icon="el-icon-edit" @click="handleCreate">
        添加
      </el-button>
    </div>
    <div class="operation-container">
      <el-table
        ref="dictTable"
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        row-key="id"
        fit
        stripe
        highlight-current-row
      >
        <el-table-column
          label="序号"
          type="index"
          :index="indexMethod"
        />
        <el-table-column label="群名称" prop="name" />
        <el-table-column label="群机器人webhook" prop="webhook" />
        <el-table-column label="秘钥" prop="sign" />
        <el-table-column label="创建者" prop="createByName" />
        <el-table-column label="创建时间" prop="createTime" width="180">
          <template slot-scope="{row}">
            <i class="el-icon-time margin-right-5" />
            <span>{{ row.createTime | dateFormat }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改者" prop="updateByName" />
        <el-table-column label="修改时间" prop="updateTime" width="180">
          <template slot-scope="{row}">
            <span>{{ row.updateTime | dateFormat }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="200" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button type="text" @click="handleUpdate(row)">
              <i class="el-icon-edit" />修改
            </el-button>
            <el-button type="text" @click="handleDelete(row)">
              <i class="el-icon-delete" />删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNumber" :limit.sync="listQuery.pageSize" @pagination="getList" />
    </div>
    <!-- 新增编辑群组 -->
    <el-dialog :title="textMap[dialogStatus]" width="600px" append-to-body :visible.sync="dialog">
      <el-form
        ref="dataForm"
        label-width="100px"
        :model="group"
        :rules="rules"
      >
        <el-form-item label="群名称" prop="name">
          <el-input v-model="group.name" />
        </el-form-item>
        <el-form-item label="群机器人webhook" prop="webhook">
          <el-input v-model="group.webhook" />
        </el-form-item>
        <el-form-item label="秘钥" prop="sign">
          <el-input v-model="group.sign" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="group.remark" maxlength="150" show-word-limit type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog = false">
          取消
        </el-button>
        <el-button type="primary" @click="submitForm">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { groupFetchList, deletes, create, update } from '@/api/data/group'
import { mapGetters } from 'vuex'
import Pagination from '@/components/Pagination'
export default {
  components: {
    Pagination
  },
  data() {
    return {
      tableKey: 0,
      dialog: false,
      dialogStatus: undefined,
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        pageNumber: 1,
        pageSize: 10
      },
      textMap: {
        update: '修改群组',
        create: '添加群组'
      },
      group: {
        id: undefined,
        name: undefined,
        sign: undefined,
        webhook: undefined,
        remark: undefined
      },
      rules: {
        name: [
          { required: true, trigger: 'blur', message: '群名称不能为空' }
        ],
        sign: [
          { required: true, trigger: 'blur', message: '群秘钥不能为空' }
        ],
        webhook: [
          { required: true, trigger: 'blur', message: '群机器人webhook不能为空' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'dict'
    ])
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      groupFetchList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 0.2 * 1000)
      })
    },
    indexMethod(index) {
      return index + 1
    },
    // 创建事件
    handleCreate() {
      this.resetFields()
      this.dialog = true
      this.dialogStatus = 'create'
    },
    // 修改事件
    handleUpdate(row) {
      this.resetFields()
      this.group = Object.assign({}, row) // copy obj
      this.dialog = true
      this.dialogStatus = 'update'
    },
    // 删除事件
    handleDelete(row) {
      deletes(row.id).then(() => {
        this.getList()
        this.$notify({
          title: '删除成功！',
          type: 'success',
          duration: 2000
        })
      })
    },
    submitForm() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.group.id) {
            update(this.group.id, this.group).then(() => {
              this.dialog = false
              this.getList()
              this.$notify({
                title: '修改成功！',
                type: 'success',
                duration: 2000
              })
            })
          } else {
            create(this.group).then(() => {
              this.dialog = false
              this.getList()
              this.$notify({
                title: '新增成功！',
                type: 'success',
                duration: 2000
              })
            })
          }
        }
      })
    },
    resetFields() {
      this.group = {
        id: undefined,
        name: undefined,
        sign: undefined,
        webhook: undefined,
        remark: undefined
      }
      if (this.$refs['dataForm'] !== undefined) {
        this.$refs['dataForm'].resetFields()
      }
    }
  }
}
</script>
